package com.dys.testio09;

import java.io.*;

/**
 * @className: src.main.java.com.dys.testio09.ObjectOutputStreamTest
 * @description: src.main.java.com.dys.testio09
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/08/16 下午11:36
 */
public class ObjectOutputStreamTest {
    /**
     * 这是一个main()方法,是应用程序的入口
     *
     * @param args 方法的形参列表,是一个字符串数组,包含了从命令行传递给程序的参数
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        new ObjectOutputStreamTest().testReadFile();
    }

    /**
     * 将引用数据类型写入到文件中
     *
     * @throws IOException
     */
    public void testOutFile() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("e:\\Demo4.txt")));
        oos.writeObject("你好");
        oos.close();
    }

    /**
     * 读取文件
     *
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void testReadFile() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("e:\\Demo4.txt")));
        String o = (String) ois.readObject();
        System.out.println(o);
        ois.close();
    }

    /**
     * 把对象转变成二进制
     *
     * @param obj 待转换的对象
     * @return 返回二进制数组
     */
    public static byte[] writeInto(Object obj) {
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            // 读取对象并转换成二进制数据
            oos.writeObject(obj);
            return bos.toByteArray();
        } catch (IOException e) {
            System.err.println("对象转换成二级制数据失败");
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 把二进制数组的数据转回对象
     *
     * @param b
     * @return
     */
    public static Object restore(byte[] b) {
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            // 读取二进制数据并转换成对象
            bis = new ByteArrayInputStream(b);
            ois = new ObjectInputStream(bis);
            return ois.readObject();
        } catch (ClassNotFoundException | IOException e) {
            System.err.println("二进制数据转回对象失败");
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
